from django.shortcuts import render, redirect
from django.views import View
from django.views.decorators.http import require_http_methods


# Create users views here.


class Home(View):

    def get(self, request):
        # 判断用户是否来过我们网站
        user_status = request.COOKIES.get('user_status')
        context = {
            'train': request.COOKIES.get('train'),
            'predict': request.COOKIES.get('predict'),
            'combination': request.COOKIES.get('combination'),
        }
        response = render(request, 'index.html', context=context)

        # 如果没来过，随机分配用户的ID存入session
        if user_status != 'is_login':
            response.set_cookie('user_status', 'is_login')
            response.set_cookie('train', 0)
            response.set_cookie('predict', 0)
            response.set_cookie('combination', 0)

        print('user_status =', user_status)
        return response


@require_http_methods('GET')
def user_logout(request):
    """退出登录清空所有统计数据"""
    response = redirect('home')
    print('退出')
    response.set_cookie('train', 0)
    response.set_cookie('predict', 0)
    response.set_cookie('combination', 0)

    return response
